package com.lxm.service;

import com.lxm.dao.EmpDao;
import com.lxm.entity.Emp;
import com.lxm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmpServiceImpl implements EmpService{
    @Autowired
    private EmpDao empDao;

    @Override
    public Result checkLogin(String name, String password) {
        Result result=new Result();
        Emp emp=empDao.findByName(name);
        if(emp==null){
            result.setStatus(0);
            result.setMsg("用户不存在！");
            return result;
        }
        if(!emp.getPwd().equals(password)){
            result.setStatus(0);
            result.setMsg("密码错误！");
            return result;
        }
        result.setStatus(1);
        result.setMsg("用户名密码正确");
        result.setData(emp);
        return result;

    }

    @Override
    public Result register(String name, String password, Integer age, Integer deptId) {
        Result result=new Result();
        Emp has_emp=empDao.findByName(name);
        if(has_emp!=null){
            result.setStatus(0);
            result.setMsg("该用户名被占用！");
            return result;
        }
        Emp emp=new Emp();
        emp.setEid(null);
        emp.setEname(name);
        emp.setEage(age);
        emp.setPwd(password);
        emp.setDeptId(deptId);
        empDao.save(emp);
        result.setStatus(1);
        result.setMsg("注册成功！");
        return result;
    }
}
